//
//  ChooseAddressViewController.m
//  Utils
//
//  Created by enghou on 2018/4/20.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "ChooseAddressViewController.h"
#import "NetWorkerManager.h"
#import "QuGrid.h"
#import "XianGrid.h"
#import "ZhenGrid.h"
#import "FrameGrid.h"
#import "LocalPersistent.h"
@interface ChooseAddressViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UIPickerView *orgPicker;

@property(nonatomic,strong)OrganizationModel *currentOrg;

@property(nonatomic,strong)UITableView *leftTableView;
@property(nonatomic,strong)NSMutableArray *leftSource;

@property(nonatomic,strong)UITableView *rightTableView;

@property(nonatomic,strong)NSMutableArray *rightSource;

@property(nonatomic,strong)XianGrid *xian;

@property(nonatomic,strong)UILabel *currentLabel;
@end

@implementation ChooseAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor defaultBackgroundColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(feedBack)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    self.title = @"当前位置";
    self.currentLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, self.view.width, 44)];
    self.currentLabel.textColor = [UIColor colorWithHexString:@"999999"];
    self.currentLabel.font = [UIFont systemFontOfSize:17];
    self.currentLabel.text = @"当前位置:";
    self.currentLabel.backgroundColor = [UIColor defaultBackgroundColor];
    [self.view addSubview:self.currentLabel];
    self.leftSource = [NSMutableArray array];
    self.leftTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 44, self.view.width / 2, self.view.height - 44) style:UITableViewStylePlain];
    self.leftTableView.delegate = self;
    self.leftTableView.backgroundColor = [UIColor whiteColor];
    self.leftTableView.dataSource = self;
    self.leftTableView.scrollEnabled = YES;
    self.leftTableView.tableFooterView = [[UIView alloc]init];
    [self.view addSubview:self.leftTableView];
    
    self.rightSource = [NSMutableArray array];
    self.rightTableView = [[UITableView alloc]initWithFrame:CGRectMake(self.view.width / 2, 44, self.view.width / 2, self.view.height - 44) style:UITableViewStylePlain];
    self.rightTableView.delegate = self;
    self.rightTableView.dataSource = self;
    self.rightTableView.scrollEnabled = YES;
    self.rightTableView.backgroundColor = [UIColor whiteColor];
    self.rightTableView.tableFooterView = [[UIView alloc]init];
    [self.view addSubview:self.rightTableView];
    
    [self loadData];
    // Do any additional setup after loading the view.
}

-(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    self.leftTableView.frame = CGRectMake(0, 44, self.view.width / 2, self.view.height - 44);
    self.rightTableView.frame = CGRectMake(self.view.width / 2, 44, self.view.width / 2, self.view.height - 44);
}

- (NSString *)xy_noDataViewMessage{
    return @"";
}

-(void)loadData{
    [self beginLoading];
    [[NetWorkerManager sharedInstance]postWithPath:kGetCurrentPositionAndGrids parameter:nil completeBlock:^(NSDictionary *source) {
        [self endLoading];
        BOOL result = [[source objectForKey:@"success"] boolValue];
        if (result) {
            NSDictionary *data = [source objectForKey:@"data"];
            XianGrid *xian = [[XianGrid alloc]initWithSource:data];
            self.xian = xian;
            [self.leftSource addObject:self.xian];
            [self.rightSource addObjectsFromArray:self.xian.subOrgs];
            [self.leftTableView reloadData];
            [self.rightTableView reloadData];
        }else{
            [self alertSome:source[@"errmsg"]];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}

-(void)feedBack{
    if (self.isMainPage) {
        //传递code
        XianGrid *grid = [self.leftSource lastObject];
        if ([self.theDelete respondsToSelector:@selector(fetchModel:)]) {
            [[LocalPersistent sharedInstance]saveContent:grid.orgCode forKey:@"orgId"];
            [self.theDelete fetchModel:grid.orgCode];
            [self pop];
        }
    }else{
        XianGrid *grid = [self.leftSource lastObject];
        if ([self.theDelete respondsToSelector:@selector(fetchModel:)]) {
            [[LocalPersistent sharedInstance]saveContent:grid.ids forKey:@"orgId"];
            [self.theDelete fetchModel:grid.ids];
            [self pop];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - UITableViewDelegate And DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
//        cell.selectionStyle = uitableviewcellselection
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:self.leftTableView]) {
        FrameGrid *f = [self.leftSource objectAtIndex:indexPath.row];
        cell.textLabel.text = f.name;
    }else if ([tableView isEqual:self.rightTableView]){
        FrameGrid *f = [self.rightSource objectAtIndex:indexPath.row];
        cell.textLabel.text = f.name;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([tableView isEqual:self.leftTableView]) {
        return [self.leftSource count];
    }else if ([tableView isEqual:self.rightTableView]){
        return [self.rightSource count];
    }
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:self.leftTableView]) {
        //设置点击的颜色
        //刷新右边的数据
        XianGrid *grid = [self.leftSource objectAtIndex:indexPath.row];
        self.rightSource = [grid.subOrgs mutableCopy];
        [self.rightTableView reloadData];
    }else if ([tableView isEqual:self.rightTableView]){
        XianGrid *grid = [self.rightSource objectAtIndex:indexPath.row];
        if ([grid isKindOfClass:[ZhenGrid class]]) {
            //左列表只能有两条
            NSMutableArray *deletedArr = [NSMutableArray array];
            [self.leftSource enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if (idx > 0) {
                    [deletedArr addObject:obj];
                }
            }];
            [self.leftSource removeObjectsInArray:deletedArr];
            //把新的Grid换上
            [self.leftSource addObject:grid];
            [self.leftTableView reloadData];
            [self.leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
            self.rightSource = [grid.subOrgs mutableCopy];
            [self.rightTableView reloadData];
        }else if ([grid isKindOfClass:[QuGrid class]]){
            NSMutableArray *deletedArr = [NSMutableArray array];
            [self.leftSource enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if (idx > 1) {
                    [deletedArr addObject:obj];
                }
            }];
            [self.leftSource removeObjectsInArray:deletedArr];
            [self.leftSource addObject:grid];
            [self.leftTableView reloadData];
            [self.leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
            self.rightSource = [grid.subOrgs mutableCopy];
            [self.rightTableView reloadData];
        }else if ([grid isKindOfClass:[FrameGrid class]]){
            NSMutableArray *deletedArr = [NSMutableArray array];
            [self.leftSource enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if (idx > 2) {
                    [deletedArr addObject:obj];
                }
            }];
            [self.leftSource removeObjectsInArray:deletedArr];
            [self.leftSource addObject:grid];
            [self.leftTableView reloadData];
            [self.leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
            self.rightSource = [grid.subOrgs mutableCopy];
            [self.rightTableView reloadData];
        }
    }
    ZhenGrid *grid = [self.leftSource lastObject];
    if (grid) {
        self.currentLabel.text = [NSString stringWithFormat:@"当前位置: %@",grid.name];
    }
}

@end

@implementation OrganizationModel


@end
